const path = require('path');
//自动创建html的插件
const HtmlWebpackPlugin = require('html-webpack-plugin');
//当都特提取csswen文件的插件
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
//压缩css的插件
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
//压缩js
const TerserPlugin = require("terser-webpack-plugin");

module.exports = {
  mode:"development",
  entry: {
    print: './src/print.js',
    index: './src/index.js',
    one: './src/one.js',
  },
  devtool: 'inline-source-map',
  devServer: {
    contentBase: './dist',
    port: 9000,
  },
  //使用插件
  plugins: [
    new HtmlWebpackPlugin({
      title: '管理输出',
      template:"./src/index.html",
    }),
    new MiniCssExtractPlugin({
      filename: "css/[name][chunkhash].css",
    })
  ],
  optimization: {
    minimizer: [
      new CssMinimizerPlugin(),
      new TerserPlugin()
    ],
  },
  output: {
    filename: 'js/[name].[chunkhash].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    clean: true,
    //assetModuleFilename: 'images/[name][hash:10][ext]'
  },
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: [MiniCssExtractPlugin.loader, "css-loader"],
        
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset',
        parser: {
          dataUrlCondition: {
            maxSize: 10 * 1024 // 4kb
          }
        },
        generator: {
          filename: 'images/[name][hash:10][ext]'
        }
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: 'asset/resource',
        generator: {
          filename: 'font/[name][hash:10][ext]'
        }
      },
    ],
  },
};
